package com.zjsru.plan2024.normal;

/**
 * 676. 实现一个魔法字典
 * @Author: cookLee
 * @Date: 2024-08-12
 */
public class MagicDictionary {

    /**
     * 主
     * \
     * 输入
     * ["MagicDictionary", "buildDict", "search", "search", "search", "search"]
     * [[], [["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]]
     * 输出
     * [null, null, false, true, false, false]
     * 解释
     * MagicDictionary magicDictionary = new MagicDictionary();
     * magicDictionary.buildDict(["hello", "leetcode"]);
     * magicDictionary.search("hello"); // 返回 False
     * magicDictionary.search("hhllo"); // 将第二个 'h' 替换为 'e' 可以匹配 "hello" ，所以返回 True
     * magicDictionary.search("hell"); // 返回 False
     * magicDictionary.search("leetcoded"); // 返回 False
     * \
     * @param args args
     */
    public static void main(String[] args) {

    }

    private String[] words;

    public MagicDictionary() {

    }

    public void buildDict(String[] dictionary) {
        words = dictionary;
    }

    public boolean search(String searchWord) {
        for (String word : words) {
            if(word.length() != searchWord.length()){
                continue;
            }

            int diff = 0;
            for (int i = 0; i < word.length(); i++) {
                if(word.charAt(i) != searchWord.charAt(i)){
                    ++ diff;
                    if(diff > 1){
                        break;
                    }
                }
            }
            if(diff == 1){
                return true;
            }
        }
        return false;
    }

}
